/*
 Source: ufxTrader.java
 Software Copyright: Hundsun Technologies Inc.
 System: UFX Future Demo (Asynchronous)
 Module: Unpackage output parameter(Trading realated)
 Describition:
 This class is receiveing trading related package from broker,and unpacking the package, the documents for function description and Java 
 programming helper can be found at ufx website: https://ufx.hscloud.cn/
 Function id include: 819706 ,819707,819709,819710,819711
 author: Kai Zhan
 Date:2016/05/13
 Others:
 This demo is a sample for using Hundsun UFX System, we are highly not recommand any user to use this demo at any real market，for more details 
 please read <T2SDK JAVA Developer Manual>
 */
package com.hmkj.core.api.ufx.trade;

import com.hundsun.t2sdk.impl.client.ClientSocket;
import com.hundsun.t2sdk.interfaces.ICallBackMethod;
import com.hundsun.t2sdk.interfaces.share.dataset.IDataset;
import com.hundsun.t2sdk.interfaces.share.dataset.IDatasets;
import com.hundsun.t2sdk.interfaces.share.event.EventReturnCode;
import com.hundsun.t2sdk.interfaces.share.event.EventTagdef;
import com.hundsun.t2sdk.interfaces.share.event.IEvent;

public class TradeCallBack implements ICallBackMethod {

    private static String glo_user_token = null;

    //抓取user_token
    public String user_token() {
        return glo_user_token;
    }

    @Override
    public void execute(IEvent arg0, ClientSocket arg1) {
        
        //获取功能号
        long iFunctionID = arg0.getIntegerAttributeValue(EventTagdef.TAG_FUNCTION_ID);
        //判断返回值
        if (arg0.getReturnCode() != EventReturnCode.I_OK) {//返回错误
           StringBuilder sb = new StringBuilder();
            //获取结果集
            IDatasets result = arg0.getEventDatas();
        	String errorInfo=arg0.getErrorInfo();
        	if(!"".equals(errorInfo)){
        		sb.append(errorInfo);
        	}else{
        		//获取结果集个数
                int datasetCount = result.getDatasetCount();
                //System.out.println(arg0.getServiceId());
                //遍历结果集
                for (int i = 0; i < datasetCount; i++) {
                    IDataset ds = result.getDataset(i);
                    int columnCount = ds.getColumnCount();
                    //打印结果集标注
                    for (int j = 1; j <= columnCount; j++) {
                        sb.append(ds.getColumnName(j));
                        sb.append("|");
                    }
                    //遍历单个结果集记录，遍历前首先将指针置到开始
                    ds.beforeFirst();
                    while (ds.hasNext()) {
                        sb.append("\n");
                        ds.next();
                        //打印结果集数据
                        for (int j = 1; j <= columnCount; j++) {
                            sb.append(ds.getString(j)).append("|");
                        }
                    }
                }
        	}
            System.err.println("以下是错误信息: \n" + sb);
        } else if (iFunctionID == 819700) {//819700 登录回调解包
            StringBuilder sb = new StringBuilder();
            //获取返回集
            IDatasets result = arg0.getEventDatas();
            //获取结果集总数
            int datasetCount = result.getDatasetCount();
            //System.out.println(arg0.getServiceId()); //打印功能号
            for (int i = 0; i < datasetCount; i++) {
                IDataset ds = result.getDataset(0);
                glo_user_token = ds.getString("user_token");//从出参中提取user_token
                int columnCount = ds.getColumnCount();//获取数据数量
                //System.out.println(arg0.getServiceId()); //打印功能号
                //打印结果集标题    
                for (int j = 1; j <= columnCount; j++) {
                    sb.append(ds.getColumnName(j));
                    sb.append("|");
                }
                //遍历单个结果集记录，遍历前首先将指针置到开始
                ds.beforeFirst();
                //打印结果集数据
                while (ds.hasNext()) {
                    sb.append("\n");
                    ds.next();
                    for (int j = 1; j <= columnCount; j++) {
                        sb.append(ds.getString(j)).append("|");
                    }
                }
            }
            System.out.println("以下是相关出参：\n" + sb);

        } else {
            StringBuilder sb = new StringBuilder();
            //获取结果集
            IDatasets result = arg0.getEventDatas();
            //获取结果集个数
            int datasetCount = result.getDatasetCount();
            //System.out.println(arg0.getServiceId());
            //遍历结果集
            for (int i = 0; i < datasetCount; i++) {
                IDataset ds = result.getDataset(i);
                int columnCount = ds.getColumnCount();
                //打印结果集标注
                for (int j = 1; j <= columnCount; j++) {
                    sb.append(ds.getColumnName(j));
                    sb.append(",");
                }
                //遍历单个结果集记录，遍历前首先将指针置到开始
                ds.beforeFirst();
                while (ds.hasNext()) {
                    sb.append("\n");
                    ds.next();
                    //打印结果集数据
                    for (int j = 1; j <= columnCount; j++) {
                        sb.append(ds.getString(j)).append(",");
                    }
                }
            }
            /*
               分时：
               contract_code|futu_exch_type|commodity_type|min_time|date|last_price|average_price|business_amount|business_balance|min_amount|
                1709|NYMEX|CL|0601|20170817|46.830000|46.810000|189|8847.490000|119|
                1709|NYMEX|CL|0602|20170817|46.810000|46.810000|228|10673.110000|39|
                1709|NYMEX|CL|0603|20170817|46.800000|46.810000|251|11749.630000|23|
                1709|NYMEX|CL|0604|20170817|46.810000|46.810000|269|12592.100000|18|
                1709|NYMEX|CL|0605|20170817|46.810000|46.810000|284|13294.170000|15|
                1709|NYMEX|CL|0606|20170817|46.800000|46.810000|303|14183.430000|19|
                1709|NYMEX|CL|0607|20170817|46.800000|46.810000|326|15259.740000|23|

             * 日k行情
             *  contract_code|futu_exch_type|commodity_type|open_price|close_price|high_price|low_price|business_amount|date|
                1709|NYMEX|CL|50.210000|48.790000|50.430000|48.370000|987058|20170801|
             1709|NYMEX|CL|48.800000|49.610000|49.650000|48.550000|828986|20170802|
             1709|NYMEX|CL|49.590000|48.930000|49.960000|48.780000|815605|20170803|
             1709|NYMEX|CL|48.950000|49.640000|48.500000|49.520000|725798|20170804|
             1709|NYMEX|CL|49.550000|49.310000|49.650000|49.290000|53077|20170807|
             1709|NYMEX|CL|49.550000|49.310000|49.650000|49.290000|53077|20170808|
             1709|NYMEX|CL|0.000000|0.000000|0.000000|0.000000|0|20170809|
             1709|NYMEX|CL|49.070000|48.510000|50.210000|48.380000|1083620|20170810|
             1709|NYMEX|CL|49.070000|48.790000|50.210000|48.010000|742944|20170811|
             1709|NYMEX|CL|48.750000|47.500000|49.130000|47.460000|735193|20170814|
             */

            System.out.println("以下是相关出参：\n" + sb);
        }
    }
 
}
